/*
 * @Author: 0x9DEFA478
 * @Date: 2024-08-04 13:23:40
 * @LastEditTime: 2024-08-10 17:24:10
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "./H_TS_MessageQueue.h"
#include "../LL/H_TS_LL.h"




static void DataCopy(volatile Hbyte* Dst,const volatile Hbyte* Src,Hsize Count){
  while(Count>7){
    Dst[0]=Src[0];
    Dst[1]=Src[1];
    Dst[2]=Src[2];
    Dst[3]=Src[3];
    Dst[4]=Src[4];
    Dst[5]=Src[5];
    Dst[6]=Src[6];
    Dst[7]=Src[7];
    Count-=8;
    Dst=&Dst[8];
    Src=&Src[8];
  }
  for(Hsize i=0;i<Count;i++){
    Dst[i]=Src[i];
  }
}

int Pop(H_TS_MessageQueue* MessageQueue,void* Message){
  Hbyte Mark;
  Hsize ReadIndex;
  int r;

  r=0;

  H_TS_Semaphore_AcquireLock(&MessageQueue->Semaphore);

  ReadIndex=MessageQueue->ReadIndex;

  if(ReadIndex!=MessageQueue->WriteIndex){
    Mark=MessageQueue->Mark[ReadIndex];
  }else{
    r=-1;
    goto lReturn;
  }

  if(Mark==0U){
    r=-1;
    goto lReturn;
  }

  {
    Hsize ItemSize;
    volatile Hbyte* Data;

    ItemSize=MessageQueue->ItemSize;
    Data=&MessageQueue->Data[ReadIndex*ItemSize];
    DataCopy(Message,Data,ItemSize);
  }

  MessageQueue->Mark[ReadIndex]=0U;

  ReadIndex++;
  if(ReadIndex>=MessageQueue->Count){
    ReadIndex=0;
  }
  MessageQueue->ReadIndex=ReadIndex;

lReturn:
  H_TS_Semaphore_AcquireUnlock(&MessageQueue->Semaphore);
  return r;
}




int H_TS_MessageQueue_TryPush_ISR(H_TS_MessageQueue* MessageQueue,void* Message){
  Hsize newWriteIndex;

  oH_TS_Assert_HandleValid(MessageQueue==NULL,"H_TS:H_TS_MessageQueue_TryPush_ISR():空的句柄");
  oH_TS_Assert_HandleValid(Message==NULL,"H_TS:H_TS_MessageQueue_TryPush_ISR():空的Message");

  newWriteIndex=H_TS_LL_AtomicAccess_WriteIndexIncrement(&MessageQueue->WriteIndex,&MessageQueue->ReadIndex,MessageQueue->Count);

  if(newWriteIndex<0){
    return -1;
  }

  {
    Hsize ItemSize;
    volatile Hbyte* Data;

    ItemSize=MessageQueue->ItemSize;
    Data=&MessageQueue->Data[newWriteIndex*ItemSize];
    DataCopy(Data,Message,ItemSize);
  }

  MessageQueue->Mark[newWriteIndex]=1U;
  H_TS_Semaphore_TryRelease_ISR(&MessageQueue->Semaphore);

  return 0;
}

int H_TS_MessageQueue_TryPush(H_TS_MessageQueue* MessageQueue,void* Message){
  Hsize newWriteIndex;

  oH_TS_Assert_HandleValid(MessageQueue==NULL,"H_TS:H_TS_MessageQueue_TryPush():空的句柄");
  oH_TS_Assert_HandleValid(Message==NULL,"H_TS:H_TS_MessageQueue_TryPush():空的Message");

  newWriteIndex=H_TS_LL_AtomicAccess_WriteIndexIncrement(&MessageQueue->WriteIndex,&MessageQueue->ReadIndex,MessageQueue->Count);

  if(newWriteIndex<0){
    return -1;
  }

  {
    Hsize ItemSize;
    volatile Hbyte* Data;

    ItemSize=MessageQueue->ItemSize;
    Data=&MessageQueue->Data[newWriteIndex*ItemSize];
    DataCopy(Data,Message,ItemSize);
  }

  MessageQueue->Mark[newWriteIndex]=1U;
  H_TS_Semaphore_TryRelease(&MessageQueue->Semaphore);

  return 0;
}

void H_TS_MessageQueue_Push(H_TS_MessageQueue* MessageQueue,void* Message){
  Hsize newWriteIndex;

  oH_TS_Assert_HandleValid(MessageQueue==NULL,"H_TS:H_TS_MessageQueue_Push():空的句柄");
  oH_TS_Assert_HandleValid(Message==NULL,"H_TS:H_TS_MessageQueue_Push():空的Message");

  while((newWriteIndex=H_TS_LL_AtomicAccess_WriteIndexIncrement(&MessageQueue->WriteIndex,&MessageQueue->ReadIndex,MessageQueue->Count))<0){
    H_TS_Semaphore_Release(&MessageQueue->Semaphore);
  }

  {
    Hsize ItemSize;
    volatile Hbyte* Data;

    ItemSize=MessageQueue->ItemSize;
    Data=&MessageQueue->Data[newWriteIndex*ItemSize];
    DataCopy(Data,Message,ItemSize);
  }

  MessageQueue->Mark[newWriteIndex]=1U;
  H_TS_Semaphore_TryRelease(&MessageQueue->Semaphore);
}

int H_TS_MessageQueue_Push_Time(H_TS_MessageQueue* MessageQueue,void* Message,H_TS_Tick Timeout){
  Hsize newWriteIndex;
  H_TS_Tick DeadLine;

  oH_TS_Assert_HandleValid(MessageQueue==NULL,"H_TS:H_TS_MessageQueue_Push_Time():空的句柄");
  oH_TS_Assert_HandleValid(Message==NULL,"H_TS:H_TS_MessageQueue_Push_Time():空的Message");
  oH_TS_Assert_ParamValid(Timeout<0,"H_TS:H_TS_MessageQueue_Push_Time():Timeout小于0");

  if(Timeout<=0){
    return H_TS_MessageQueue_TryPush(MessageQueue,Message);
  }

  DeadLine=H_TS_LL_GetRunningTick()+Timeout;
  while((newWriteIndex=H_TS_LL_AtomicAccess_WriteIndexIncrement(&MessageQueue->WriteIndex,&MessageQueue->ReadIndex,MessageQueue->Count))<0){
    H_TS_Tick timeout;
    timeout=DeadLine-H_TS_LL_GetRunningTick();
    if(timeout<=0){
      break;
    }
    H_TS_Semaphore_Release_Time(&MessageQueue->Semaphore,timeout);
  }

  if(newWriteIndex<0){
    return -1;
  }
  
  MessageQueue->Mark[newWriteIndex]=1U;
  H_TS_Semaphore_TryRelease(&MessageQueue->Semaphore);

  return 0;
}

int H_TS_MessageQueue_TryPop(H_TS_MessageQueue* MessageQueue,void* Message){
  
  oH_TS_Assert_HandleValid(MessageQueue==NULL,"H_TS:H_TS_MessageQueue_TryPop():空的句柄");
  oH_TS_Assert_HandleValid(Message==NULL,"H_TS:H_TS_MessageQueue_TryPop():空的Message");

  if(Pop(MessageQueue,Message)!=0){
    return -1;
  }

  H_TS_Semaphore_TryAcquire(&MessageQueue->Semaphore);
  return 0;
}

void H_TS_MessageQueue_Pop(H_TS_MessageQueue* MessageQueue,void* Message){
  
  oH_TS_Assert_HandleValid(MessageQueue==NULL,"H_TS:H_TS_Queue_Pop():空的句柄");
  oH_TS_Assert_HandleValid(Message==NULL,"H_TS:H_TS_Queue_Pop():空的Message");

  while(Pop(MessageQueue,Message)!=0){
    H_TS_Semaphore_Acquire(&MessageQueue->Semaphore);
  }

  H_TS_Semaphore_TryAcquire(&MessageQueue->Semaphore);
}


int H_TS_MessageQueue_Pop_Time(H_TS_MessageQueue* MessageQueue,void* Message,H_TS_Tick Timeout){
  int r;
  H_TS_Tick DeadLine;

  oH_TS_Assert_HandleValid(MessageQueue==NULL,"H_TS:H_TS_MessageQueue_Pop_Time():空的句柄");
  oH_TS_Assert_HandleValid(Message==NULL,"H_TS:H_TS_MessageQueue_Pop_Time():空的Message");

  if(Timeout<=0){
    return H_TS_MessageQueue_TryPop(MessageQueue,Message);
  }

  DeadLine=H_TS_LL_GetRunningTick()+Timeout;
  while((r=Pop(MessageQueue,Message))!=0){
    H_TS_Tick timeout;
    timeout=DeadLine-H_TS_LL_GetRunningTick();
    if(timeout<=0){
      break;
    }
    H_TS_Semaphore_Acquire_Time(&MessageQueue->Semaphore,timeout);
  }

  H_TS_Semaphore_TryAcquire(&MessageQueue->Semaphore);
  return r;
}



